package com.wiloon.java8x.thread;

/**
 * Created by wiloon on 3/26/17;
 */
public class InputOutputDemo{
    public static void main(String args[]){
        Student stu = new Student();
        Input in = new Input(stu);
        Output out = new Output(stu);
        Thread t1 = new Thread(in);  //存入学生信息的线程
        Thread t2 = new Thread(out); //打印学生信息的线程
        t1.start();
        t2.start();
    }
}
class Input implements Runnable{
    private Student stu;
    boolean flag = false;
    public Input(Student stu){
        this.stu = stu;
    }
    public void run(){//这个线程的run方法负责将姓名和性别存入
    /*在这里定义两个学生切换着存入学生的信息
    */
        while(true){
            if(flag){
                stu.name = "ab";
                stu.sex = "female";
                flag = false;
            }else{
                stu.name = "cd";
                stu.sex = "male";
                flag = true;
            }
        }
    }
}
class Output implements Runnable{
    private Student stu;
    public Output(Student stu){  //保证传入对象唯一性
        this.stu = stu;
    }
    public void run(){
        while(true)
            System.out.println(stu.name+"..."+stu.sex);
    }
}
class Student{
    String name;
    String sex;
}